package com.zhaungjie.base.utils;

import com.zhaungjie.base.utils.PropertiesCopyUtils.PropertiesCopy;

/**
 * 类型转换兼容utils
 *
 * @author zhuangjie
 * @date 2024/01/26
 */
public class TypeConvertCompatibleUtils {
    public static<T> T convert(Object obj, Class<T> clazz) {
        // 基本类型的判断转换
        if (obj == null) {
            return null;
        }else if (clazz.isInstance(obj)) {
            return clazz.cast(obj);
        }else if (clazz == String.class) {
            return (T)String.valueOf(obj);
        }else if(clazz == Integer.class) {
            return (T)Integer.valueOf((String)obj);
        }else if(clazz == Long.class) {
            return (T)Long.valueOf((String)obj);
        }else if(clazz == Double.class) {
            return (T)Double.valueOf((String)obj);
        }else if(clazz == Byte.class) {
            return  (T)Byte.valueOf((String)obj);
        }else if(clazz == Short.class) {
            return   (T)Short.valueOf((String)obj);
        }else if(clazz == Float.class) {
            return  (T)Float.valueOf((String)obj);
        }else if(clazz == Boolean.class) {
            return   (T)Boolean.valueOf((String)obj);
        }else if (clazz == Character.class) {
            return (T)(Character) ((String)obj).charAt(0);
        }else {
            // 进行属性拷贝
            return PropertiesCopy.type(Object.class,clazz).produce(obj);
        }
    }

    public static void main(String[] args) {
        Integer convert = TypeConvertCompatibleUtils.convert("123", Integer.class);
        System.out.println(convert);
    }
}
